<template>
    <div class="login">

        <!--滑块验证-->
        <el-card class="cover" v-if="loginAdmin.id">
            <slide-verify :l="42"
                          :r="10"
                          :w="310"
                          :h="155"
                          slider-text="向右滑动"
                          @success="onSuccess"
                          @fail="onFail"
                          @refresh="onRefresh"
            ></slide-verify>
        </el-card>

            <div style="height: 100vh; overflow: hidden  "  >
                <div class="building">
                    <div style="margin:30px;text-align: center;font-size: 40px;font-weight: bold;color: black;font-family: 楷体">登录</div>
                    <el-form :model="admin"  ref="loginForm" :rules="rules" >
                        <el-form-item prop="username" label-width="70px">
                            <el-input  style="width: 250px " placeholder="请输入账号" prefix-icon="el-icon-user " size="medium" v-model="admin.username"></el-input>
                        </el-form-item>
                        <el-form-item prop="password" label-width="70px">
                            <el-input style="width: 250px " placeholder="请输入密码" show-password prefix-icon="el-icon-lock " size="medium" v-model="admin.password"></el-input>
                        </el-form-item>
                        <el-form-item >
                            <el-button style="margin-left: 110px" size="medium" type="primary" @click="login">
                                登录
                            </el-button>
                          <el-button size="medium" type="success" @click="$router.push('/register')">
                            注册
                          </el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>

</template>

<script>
    import request from "@/util/request";
    import Cookies from 'js-cookie';
    export default {
        name:"LOGIN",
        data(){

            return{
                admin:{},
                loginAdmin:{},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password:[
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
            }
        },
        methods:{
            login(){
                this.$refs['loginForm'].validate((valid) => {
                    if (valid) {
                        request.post("/admin/login",this.admin).then(res=>{
                            if (res.code=="200"){
                                this.loginAdmin=res.data//登陆成功，滑块出现
                              console.log(this.loginAdmin)
                               }else{
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                })

            },
            onSuccess() {
                Cookies.set('admin', JSON.stringify(this.loginAdmin))//把当前登录用户信息存到cookies当中
                this.$notify.success("登录成功")
              console.log(this.loginAdmin)
              if (this.loginAdmin.identity==1){

                this.$router.push('/hello')//跳转到首页
              }else{
                this.$router.push('/')
              }

            },
            onFail() {

            },
            onRefresh() {
                console.log('refresh')
            }


        }
    }

</script>
<style>
    .login{
        /* 背景图不平铺 */
        /* 当内容高度大于图片高度时，背景图像的位置相对于viewport固定 */
        background: url("../../assets/register.jpg") no-repeat fixed center center;
        /* 让背景图基于容器大小伸缩 */
        background-size: cover;
        /* 设置背景颜色，背景图加载过程中会显示背景色 */


    }
    .building{
        background: rgba(255, 255, 255, 0.3) ;
        width:380px;
        height:250px;
        margin-left: 720px;
        margin-top: 300px;



    }
    .cover {
        width: fit-content;
        background-color: white;
        position: absolute;
        top:50%;
        left:50%;
        transform: translate(-50%, -50%);
        z-index: 1000;
    }

</style>
